<?php

/**
 * SystemController.class.php 后台应用管理基类
 * Created Time 2016年3月10日 下午5:58:48
 *
 * @author     late.xiao@qq.com
 */
namespace System\Common;

use Base\Lib\BaseController;

class SystemController extends BaseController {
    protected $system_title = '后台管理';
    protected $system_left_tpl = 'System@Public:left';
    protected $system_left_menu = array();
    protected $app_menu = array();
    protected $permission = true;
    
    public function __construct()
    {
        parent::__construct();
        // 判断是否登录
        $bool = $this->__checkManger();
        if(!$bool)
        {
            if(IS_AJAX)
            {
                return $this->error('请登录', U('Sysmanger/Passport/login'), true);
            }
            $url = U('Sysmanger/Passport/login');
            return redirect($url);
        }
        
//         // 判断权限
        $bool = $this->__checkPermission();
        if(!$bool)
        {
            $msg = '您没有权限进行此操作';
            if(IS_AJAX)
            {
                return $this->error($msg,null, true);
            }
            return $this->error($msg);
        }
    }

    public function loadSystemView($templateFile = '', $charset = '', $contentType = '', $content = '', $prefix = '')
    {

        $this->assign ('system_title', $this->system_title);
        $this->assign('current_app', $this->identifier);
        $this->assign('is_login', true);
        $this->assign('leftmenu', $this->system_left_menu);
        
        // 设置app菜单
        $this->_getMenu();
        if($this->app_menu)
        {
            $app_menu = $this->app_menu;
            $this->assign('left_menu', $app_menu['left']);
            $this->assign('top_menu', $app_menu['top']);
        }
        
        // 设置后台模块模板
        $this->display ('System@Public:header', $charset, $contentType, $content, $prefix);
        $this->display ($this->system_left_tpl, $charset, $contentType, $content, $prefix);
        $this->display ('System@Public:right', $charset, $contentType, $content, $prefix);
        $this->display ($templateFile, $charset, $contentType, $content, $prefix);
        $this->display ('System@Public:footer', $charset, $contentType, $content, $prefix);
    }
    
    protected function _getMenu()
    {
        
        $menu = array();
        $menuLink = array();
        $top_tmp = array();
        $left_menu = array();
        $menu = C('menu-list');
        
        
        // 获取头部菜单
        foreach ($menu as $val)
        {
            if($val['display'])
            {
                $top_menu['lable'] = $val['lable'];
                $tmp_var = $this->__formatUses($val['uses']);
                list($modul, $ctl, $method,$link) = $tmp_var;
                
                $top_menu['module'] = $modul;
                $top_menu['ctl'] = $ctl;
                $top_menu['act'] = $method;
                $top_menu['link'] = $link;
                $top_menu['icon'] = $val['icon'];
                $top_tmp[] = $top_menu;
                
            }
        }
        // 获取左侧菜单
        $modul = MODULE_NAME;
        $left_menu = $menu[$modul];
        $left_tmp = array();
        if(isset($left_menu['menu']) && $left_menu['menu'])
        {
            foreach ($left_menu['menu'] as $val)
            {
                if($val['display'])
                {
                    $tmp_var = $this->__formatUses($val['uses']);
                    list($modul, $ctl, $method,$link) = $tmp_var;
                    $left['module'] = $modul;
                    $left['ctl'] = $ctl;
                    $left['act'] = $method;
                    $left['link'] = $link;
                    $left['lable'] = $val['lable'];
                    $left_tmp[] = $left;
                }
            }
        }
        
        $list['top'] = $top_tmp;
        $list['left'] = $left_tmp;
        $this->app_menu = $list;
        
        return true;
    }

    protected function _assignUeditor($defaultContent='', $contentKey='content', $options = array(), $toolBars = '_mobile', $innerHight = 300)
    {
        $ueditor = \Component\Common\Ueditor::get_instance($options);
        // $contentKey = 'content';
        $ueditor->ueditor_config = array('toolbars' => $toolBars, 'textarea' => $contentKey, 'initialFrameHeight' => $innerHight, 'initialContent' =>$defaultContent, 'elementPathEnabled' => false);
        if (! $ueditor->create_editor ('content'))
        {save_app_log($ueditor->ueditor_error, $this->identifier);}
        $ueditor_html = $ueditor->ueditor_html;
        $this->assign ('ueditor', $ueditor_html);
    
        return true;
    }
    
    protected function _assignPage($count, $limit, $filter=array())
    {
        $pager = new \Think\Page($count, $limit);
        
        if($filter)
        {
            foreach($filter as $key=>$val) {
                $pager->parameter[$key]   =   $val;
            }
        }
        
        $this->assign('pager', $pager->show());
        return true;
    }
    
    // 获取权限列表
    protected function _getPermissonList()
    {
        $menu_list = C('menu-list');
        $tmp_arr = array();
        foreach ($menu_list as $key=>$val)
        {
            if(isset($val['menu']))
            {
                $tmp_arr = array_merge($tmp_arr, $val['menu']);
            }
    
        }
    
        $permisson = array();
        foreach ($tmp_arr as $val)
        {
            if($val['permission'])
            {
                $permisson[] = $val;
            }
        }
    
        return $permisson;
    }
    
    private function __formatUses($str)
    {
        if(!$str) return false;
        
        list($modul, $ctl, $method) = explode('@', $str);
        
        $link = '%s/%s/%s';
        $link = sprintf($link, $modul, $ctl, $method);
        $link = U($link);
        return array($modul, $ctl, $method,$link);
        
    }
    
    private function __checkManger()
    {
        $manger_info = session('manger');
        if(!$manger_info)
        {
            return false;
        }
        
        $now_time = NOW_TIME;
        if(($now_time-$manger_info['set_time']) >= $manger_info['expire'])
        {
            return false;
        }
        
        return true;
    }
    
    // 检查权限
    private function __checkPermission()
    {
        $current_uses = '%s@%s@%s';
        $current_uses = sprintf($current_uses,MODULE_NAME,CONTROLLER_NAME,ACTION_NAME);
        
        $menu_list = C('menu-list');
        foreach ($menu_list as $key=>$val)
        {
            if($val['uses']==$current_uses && !$val['permission'])
            {
                return true;
            }
            
            if(isset($val['menu']))
            {
                foreach ($val['menu'] as $v)
                {
                    if($v['uses'] == $current_uses && !$v['permission'])
                    {
                        return true;
                    }
                }
            }
        }
        
        // 判断管理员权限
        $role_id = session('manger.role_id');
        if($role_id == 0)
        {
            return true;
        }
        
        // 获取管理员权限
        $obj_serv = D('Sysmanger/Manger', 'Service');
        $result = $obj_serv->getRole($role_id, 'permission');
        
        // 如果没有这个管理员的角色信息，那就强制退出
        if(!$result)
        {
            session('manger', null);
            return $this->redirect('Sysmanger/Passport/login');
        }
        
        $per_list = explode(',', $result['permission']);
        if(in_array($current_uses, $per_list))
        {
            return true;
        }
        
        return false;
    }
    

}